// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use bwexam::cmd;
use std::env;
use tauri::WindowEvent;

// 初始化脚本
const INIT_SCRIPT: &str = include_str!("init.js");

fn main() {
    println!("OPENCV_LINK_PATHS: {}", env!("OPENCV_LINK_PATHS"));

    // 判断参数来源

    // 获取命令行参数
    let args: Vec<String> = env::args().collect();

    // 打印程序名
    println!("Program name: {}", args[0]);

    // 打印其他参数
    for arg in args.iter().skip(1) {
        println!("{}", arg);
    }
    
    // use tauri::{CustomMenuItem, Menu, MenuItem, Submenu};
    // let quit = CustomMenuItem::new("quit".to_string(), "退出");
    // let close = CustomMenuItem::new("close".to_string(), "关闭");
    // let submenu = Submenu::new("File", Menu::new().add_item(quit).add_item(close));
    // let menu = Menu::new()
    //     .add_native_item(MenuItem::Copy)
    //     .add_item(CustomMenuItem::new("hide", "Hide"))
    //     .add_submenu(submenu);

    tauri::Builder::default()
        .setup(|app| {
            // let window = tauri::WindowBuilder::new(app, "main", tauri::WindowUrl::App("index1.html".into()))
            let window = tauri::WindowBuilder::new(
                app,
                "main",
                // tauri::WindowUrl::External("https://www.baidu.com".parse().unwrap()),
                // https://blog.csdn.net/weixin_42842069/article/details/134537690
                // 【rust：tauri-app踩坑记录】dangerousRemoteDomainIpcAccess 不适用于IP地址，临时解决方案
                //  patch 打在  dll\tauri-1.7.1\src\scope\ipc.rs
                // tauri::WindowUrl::External("http://172.16.10.111:5886".parse().unwrap()),
                tauri::WindowUrl::External("http://localhost:8081/index1.html".parse().unwrap()),
            )
            .title("八维考试系统")
            .initialization_script(INIT_SCRIPT)
            .build()?;
            window.open_devtools();

            Ok(())
        })
        //.menu(menu)
        // .on_menu_event(|event| match event.menu_item_id() {
        //     "quit" => {
        //         std::process::exit(0);
        //     }
        //     "close" => {
        //         event.window().close().unwrap();
        //     }
        //     _ => {}
        // })
        .on_window_event(move |event| match event.event() {
            WindowEvent::Destroyed => {
                // https://github.com/tauri-apps/tauri/discussions/3273
                println!("xxxxxxxx====WindowEvent  Destroyed");
                // 这里需要处理边界问题
                cmd::stop_ffmpeg();
            }
            _ => {}
        })
        .invoke_handler(tauri::generate_handler![
            cmd::greet,
            cmd::check_screen,
            cmd::set_save_path,
            cmd::set_output_filename,
            cmd::start_ffmpeg,
            cmd::stop_ffmpeg,
            cmd::start_edog,
            cmd::stop_edog
        ])
        .run(tauri::generate_context!())
        .expect("error while running tauri application");
}
